﻿
@{
    Layout = null;
}

@model Model.Bills
@using Model;

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
    <link href="~/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    <div style="margin:10px">
        <form  lay-filter="formTest"  class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">类型：</label>
                <div class="layui-input-block">
                    @{
                        foreach (var item in ViewBag.BillTypeList as List<Bill_Type>)
                        { <input type="radio" name="Bill_Type_Id" value="@item.Id" title="@item.TypeName">}
                    }
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">标题：</label>
                <div class="layui-input-block">
                    @Html.HiddenFor(bill => bill.Id, new { value = 0 })
                    @Html.TextBoxFor(bill => bill.Title, new { @class = "layui-input" })
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">日期</label>
                    <div class="layui-input-inline">
                        @Html.TextBoxFor(bill => bill.Bill_time, new
                        {
                            @class = "layui-input",
                       placeholder = "yyyy-MM-dd HH:mm:ss"
                        })
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">金额</label>
                    <div class="layui-input-inline">
                        @Html.TextBoxFor(bill => bill.Price, new { @class = "layui-input" })
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">说明：</label>
                <div class="layui-input-block">
                    @Html.TextAreaFor(bill => bill.Explain, new { @class = "layui-textarea" })
                </div>
            </div>
            <div class="layui-form-item" style="text-align:center">
                <button type="reset" class="layui-btn layui-btn-normal layui-btn-sm">重置</button>
                <button type="button" lay-submit lay-filter="submit"
                        class="layui-btn layui-btn-normal layui-btn-sm">
                    保存
                </button>
                <button type="button" id="btnClose" class="layui-btn layui-btn-normal layui-btn-sm">关闭</button>
            </div>

        </form>
    </div>
</body>
</html>
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/layui/layui.all.js"></script>
<script>

    let form = null;

    layui.use(["form", 'laydate'], function () {

         form = layui.form;  //创建表格对象
        let laydate = layui.laydate; //创建日期对象


        //日期时间选择器
        laydate.render({
            elem: '#Bill_time'
            , type: 'datetime'
        });


        //提交事件
        form.on('submit(submit)', function (data) {

            var bills = data.field;

            $.post("/Bill/SaveBillsInfo", { bills })
                .then(res => {
                    if (res > 0) {
                        layer.alert("保存成功！", { icon: 1 }, function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭

                            //刷新父级窗体
                            parent.location.reload();
                        })
                    }
                })

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //初始化窗体数据
        initPage(@ViewBag.id);
    });

    $(function () {
        $("#btnClose").on("click", () => {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });
    })

    function initPage(id) {

        if (@ViewBag.id== 0)
            return;

        $.post("/Bill/GetBillssInfoById", { id: id})
            .then(res => {
                form.val("formTest", res);

                $("#Bill_time").val(formartDateTime(res.Bill_time))
            })
    }


    function formartDateTime(val) {
        if (val != null) {
            var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
            //月份为0-11，所以+1，月份小于10时补个0
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            var dd = date.getFullYear() + "-" + month + "-" + currentDate + " " + hour + ":" + minute + ":" + second;

            return dd;
        }

        return "";
    }
</script>